package com.song.shop.nio;

import java.io.*;
import java.net.Socket;

/**
 * @author Song Yinghui
 * @date 2019/2/13
 * @Version 1.0
 */
public class ThreadUtils extends Thread {

    public  ThreadUtils(String msg){
        Runnable runnable = () -> {
            try {
                Socket socket = new Socket("127.0.0.1" , 8888);
                OutputStream outputStream = socket.getOutputStream();
                InputStream inputStream = socket.getInputStream();
                outputStream.write(msg.getBytes());
                outputStream.flush();


                System.out.println("进入接受消息代码-----");
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader br =new BufferedReader(inputStreamReader);
                String info = br.readLine() ;
                while ( info != null){
                    System.out.println(info);
                    info = br.readLine() ;
                }
                inputStream.close();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        };

        new Thread(runnable).start();
    }
}
